<template>
  <el-dialog class='subject-status-add-record' :title="dialogTitle" width="600px" :show-close='true'
             :visible.sync="dialogVisible" :close-on-click-modal="false" :before-close="handleClose">
    <div>
      <el-form :rules="rules" ref="formRef" size="mini" :model="formData" label-width="140px">
        <el-form-item label="研究代码：">
          <el-input style="width: 80%" disabled clearable placeholder="请输入" v-model.trim="formData.projectCode">
          </el-input>
        </el-form-item>
        <el-form-item label="提示语名称：" prop="hintName">
          <el-input style="width: 80%" :disabled="dialogTitle === '编辑'" clearable placeholder="请输入" v-model.trim="formData.hintName">
          </el-input>
        </el-form-item>
        <el-form-item label="提示语类型：" prop="hintType">
          <el-input style="width: 80%" :disabled="dialogTitle === '编辑'" clearable placeholder="请输入" v-model.trim="formData.hintType">
          </el-input>
        </el-form-item>
        <el-form-item label="提示语设置：" prop="hintConfig">
          <el-input style="width: 80%" clearable placeholder="请输入" v-model.trim="formData.hintConfig">
          </el-input>
        </el-form-item>
        <el-form-item label="提示语说明：" prop="hintDesc">
          <el-input style="width: 80%" clearable placeholder="请输入" v-model.trim="formData.hintDesc">
          </el-input>
        </el-form-item>
        <el-form-item v-if="dialogTitle=== '复制'">
          <p style="color: red;"><i class="el-icon-info"></i>复制前请先修改数据！</p>
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="handleClose">取 消</el-button>
      <el-button :loading='loading' size="small" type="primary" @click="handleSave">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { mapActions } from 'vuex'

export default {
  props: {
    editData: Object,
    dialogVisible: Boolean,
    dialogTitle: String,
    questioningModeList: Array,
    searchParams: Object
  },
  data() {
    return {
      rules: {
        hintName: [{ required: true, message: '请输入', trigger: 'blur' }],
        hintType: [{ required: true, message: '请输入', trigger: 'blur' }],
        hintConfig: [{ required: true, message: '请输入', trigger: 'blur' }],
        hintDesc: [{ required: true, message: '请输入', trigger: 'blur' }],
      },
      loading: false,
      defaultFormData: {},
      formData: {}
    }
  },

  watch: {
    dialogVisible(val) {
      if (val) {
        this.formData = this.editData.id ? { ...this.editData } : { ...this.editData, ...this.defaultFormData }
      } else {
        this.formData = { ...this.defaultFormData }
      }
    },
  },

  methods: {
    ...mapActions('verificationRule', ['editECPrompt', '', 'addECPrompt']),

    // 关闭取消
    handleClose() {
      this.formData = { ...this.defaultFormData }
      this.$emit('handleCancel')
    },

    // 保存提交
    handleSave() {
      this.loading = true
      this.$refs.formRef.validate((valid) => {
        if (valid) {
          const data = {
            ...this.searchParams,
            ...this.formData
          }
          if (this.dialogTitle === '编辑') {
            this.editECPrompt({ data }).then(res => {
              const { code } = res
              if (code === 200) {
                this.$message({
                  message: '保存成功',
                  type: 'success'
                })
                this.$refs.formRef.resetFields()
                this.$emit('handleCancel', 'refresh')
              }
              this.loading = false
            })
          } else {
            this.addECPrompt({ data }).then(res => {
              const { code } = res
              if (code === 200) {
                this.$message({
                  message: '保存成功',
                  type: 'success'
                })
                this.$refs.formRef.resetFields()
                this.$emit('handleCancel', 'refresh')
              }
              this.loading = false
            })
          }
        } else {
          this.loading = false
          return false
        }
      })
    },
  }
}
</script>
<style lang="scss" scoped>
@import './edit-record.scss';
</style>
